<!DOCTYPE html>
<html xmlns:th="http://www.springframework.org/schema/jdbc">
<head>
    <meta charset="utf-8">
    <title>添加系统用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-col-md12">
    <div class="layui-card" id="ly-form">
        <div class="layui-card-header">操作员信息</div>
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-inline">
                            <input type="password" id="passwd" name="passwd" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="nickname" name="nickname" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <input type="text" id="remark" name="remark" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" id="DepartmentView"></div>
                <script id="DepartmentTemplate" type="text/html">
                    <label class="layui-form-label" required>{{ d.label }}</label>
                    <div class="layui-input-block" style="height: 38px;">
                        <select id="parent" name="parent" lay-verify="required" lay-search>
                            {{# layui.each(d.list, function(index, item){ }}
                            <option value="{{item.id}}">{{ item.name }}</option>
                            {{# }); }}
                        </select>
                    </div>
                </script>
                <div class="layui-form-item" pane="">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input type="radio" id="role1" name="role" value="1" title="超管" disabled="">
                        <input type="radio" id="role2" name="role" value="2" title="管理员">
                        <input type="radio" id="role3" name="role" value="3" title="用户" checked="">
                    </div>
                </div>
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="实时监控">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="视频预览">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="数据监控">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="系统参数">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="基本参数">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="数据库">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="服务器设置">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="企业管理">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="企业信息">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="部门信息">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="系统操作员">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="人员管理">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="添加人员">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="人事管理">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="人员授权">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="人员同步">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="设备名单">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="数据查询">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="考勤数据">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="人脸记录">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="刷卡记录">-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item" pane="">-->
                    <!--<label class="layui-form-label"></label>-->
                    <!--<div class="layui-input-block">-->
                        <!--<input type="checkbox" name="system[1]" lay-skin="primary" title="设备管理">-->
                        <!--<input type="checkbox" name="system[2]" lay-skin="primary" title="搜索设备">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="添加设备">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="摄像机管理">-->
                        <!--<input type="checkbox" name="system[3]" lay-skin="primary" title="控制板管理">-->
                    <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(['form', 'table', 'laytpl', 'layer'], function () {
        let $ = layui.jquery,
            layer = layui.layer,
            laytpl = layui.laytpl,
            form = layui.form;

        $("input").prop("disabled", false);
        $(".layui-input-block input").prop("disabled", false);

        function selectTemplate(data, name, label, templateId, viewId) {
            let list = {"name": name, "label": label,"list":data};
            let getTpl = document.getElementById('' + templateId + '').innerHTML;
            let view = document.getElementById('' + viewId + '');
            laytpl(getTpl).render(list, function (html) {
                view.innerHTML = html;
            });
            form.render('select');
        }

        $(document).ready(function () {
            let user = [[${user}]];
            let departments = [[${departments}]];
            selectTemplate(departments, "parent", "所属部门", "DepartmentTemplate", "DepartmentView");
            if (user != null){
                $("#name").val(user.name);
                $('#passwd').val(user.passwd);
                $('#nickname').val(user.nickname);
                $('#remark').val(user.remark);
                $('#role').val(user.role - 1);//这是子页面的单选按钮，让其选中
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:`/addOperator`,
                type:`post`,
                dataType:`json`,
                data: data.field ,
                success:function (res) {
                    console.log(res);
                    if (res.code == 200){
                        layer.close(layer.index);
                        window.parent.location.reload();
                    }else{
                        layer.msg("添加管理员失败，请重试");
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>